//banner轮播
var t = n = 0;
var count;
var oss_url = 'http://xhl.oss-cn-shanghai.aliyuncs.com/';
function bannerInit() {
    $.ajax({
        type: "post",
        url: "/userweb/Homepage/bannerList",
        cache: false,
        async:false,
        data: {},
        dataType: "json",
        success: function (data) {
            if (200 == data.status) {
                var images = '';
                var on = '';
                for (var i=0;i<data.bannerImages.length;i++){
                    data.bannerImages[i] = oss_url + data.bannerImages[i];
                    images += '<a href="javascript:;">';
                    images += '<img src="'+data.bannerImages[i]+'" title="" alt="">';
                    images += '</a>';
                    if (i==0){
                        on += '<li class="on" onclick="showList('+i+')"></li>';
                    }else {
                        on += '<li onclick="showList('+i+')"></li>';
                    }
                }
                $('.banner_list').html(images);
                $('#on').html(on);
            }
        }
    });
    count = $(".banner_list a").length;
    var bannerLeft = 600 -(count *28)/2;
    $(".banner ul").css('left',bannerLeft);

    $(".banner_list a:not(:first-child)").hide();
    t = setInterval("showAuto()", 4000);
    $(".banner").hover(function () {
            clearInterval(t)
        },
        function () {
            t = setInterval("showAuto()", 4000);
    });
}
function showList(m) {
    var i = m;
    n = i;
    if (i >= count) return;
    $(".banner_list a").filter(":visible").fadeOut(500).parent().children().eq(i).fadeIn(500);
    $(".banner li").eq(i).toggleClass("on").siblings().removeAttr("class");
}
function showAuto() {
    n = n >= (count - 1) ? 0 : ++n;
    showList(n);
}
//input提示隐藏
function hiddenDiv(dom){
    $(dom).css('display','none');
    $(dom).prev("input").css('display','block');
    $(dom).prev("input").focus();
    $(dom).prev("input").val("");
}
//input失去焦点显示提示
function showInput(dom){
    var text = $(dom).val();
    if(!text){
        $(dom).css('display','none');
        $(dom).next('div').css('display','block');
    }else{
        $(dom).css('display','block');
        $(dom).next('div').css('display','none');
    }
}
/**
 * 从地址栏获取参数.
 * @param url
 * @param name
 * @returns {*}
 */
function getUrlParam(url,name) {
    var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
    var matcher = pattern.exec(url);
    var items = null;
    if(null != matcher){
        try{
            items = decodeURIComponent(decodeURIComponent(matcher[1]));
        }catch(e){
            try{
                items = decodeURIComponent(matcher[1]);
            }catch(e){
                items = matcher[1];
            }
        }
    }
    return items;
}

/**
 *  打星星
 * @param num
 * @returns {string}
 */
function star(num) {
    var num1    = Math.ceil(num);      //取整数(实心)
    var num2    = 5 - num1;  //取整数(空心)
    var html = '';
    for(var i=1;i<=num1;i++){
        html += '<div><img src="/image/userweb/slicesImg/star-fill.png" alt=""></div>';
    }
    for(var i=1;i<=num2;i++){
        html += '<div><img src="/image/userweb/slicesImg/star-area.png" alt=""></div>';
    }
    return html;
}

/**
 *  页面刷新验证token
 * @param num
 * @returns {string}
 */
function validToken() {
    var user_id     = localStorage.getItem('user_id');
    var web_token   = localStorage.getItem('web_token');
    var data = {
        user_id : user_id
    };
    $.ajax( {
        url : "/userweb/Homepage/validToken" ,
        dataType : 'json' ,
        type : 'POST' ,
        data : data ,
        success : function ( res ) {
            if (res.web_token != web_token){
                localStorage.removeItem('user_id');
                localStorage.removeItem('user_name');
                localStorage.removeItem('user_image');
                localStorage.removeItem('web_token');
                localStorage.removeItem('city');
                layer.msg('已在其他设备登录',{
                    skin:'winning-class',
                    time:3000
                });
                setTimeout(function () {
                    location.href = "/userweb/index/login.html";
                }, 2000);
            }
        } ,
        error : function () {

        }
    } )
}

/**
 * 增加商店浏览次数
 * @param num
 * @returns {string}
 */
function browseNum(supplier_id) {
    var user_id     = localStorage.getItem('user_id');
    if (user_id){
        var data = {
            user_id: user_id,
            supplier_id: supplier_id
        };
        $.ajax( {
            url : "/api/Store/browseNum" ,
            dataType : 'json' ,
            type : 'POST' ,
            data : data ,
            success : function ( res ) {
                //console.log(res);
            } ,
            error : function () {

            }
        } )
    }
}

/**
 * 购物数据存浏览器缓存
 * @param data
 */
function buy_car_add(data){
    // var car = localStorage.setItem('buy_car','');
    var car = localStorage.getItem('buy_car');
    if(null == car || undefined == car || '' == car || [] == car || car =='null'||car =='undefined'){
        car = [];
        car.push(data);
    }else{
        car = JSON.parse(car);
        var is_supplier = false;
        for(var i = 0;i<car.length;i++){
            if(car[i].supplier_id == data.supplier_id){
                is_supplier = true;
                var is_product = false;
                for(var j = 0 ;j<car[i].product.length;j++){
                    if(car[i].product[j].product_id == data.product[0].product_id){
                        if(car[i].product[j].specification==data.product[0].specification){
                            car[i].product[j].num       = parseInt(car[i].product[j].num) + parseInt(data.product[0].num);
                            if (car[i].product[j].num>data.product[0].stock){
                                car[i].product[j].num       =  parseInt(data.product[0].stock);
                                var total_tax = (parseFloat(data.product[0].tax) * parseInt(data.product[0].specification) * parseInt(data.product[0].stock)).toFixed(2);
                                var total_price = (parseFloat(data.product[0].price) * parseInt(data.product[0].specification) * parseInt(data.product[0].stock)).toFixed(2);
                                car[i].product[j].total_tax     =   total_tax;
                                car[i].product[j].total_price   =   (parseFloat(total_tax) + parseFloat(total_price)).toFixed(2);
                            }else {
                                car[i].product[j].total_tax = (parseFloat(car[i].product[j].total_tax)      + parseFloat(data.product[0].total_tax)).toFixed(2);
                                car[i].product[j].total_price = (parseFloat(car[i].product[j].total_price)  + parseFloat(data.product[0].total_price)).toFixed(2);
                            }
                        }else{
                            car[i].product.push(data.product[0]);
                            /*car[i].product[j].specification = data.product[0].specification;
                            car[i].product[j].price         = data.product[0].price;
                            car[i].product[j].num           = data.product[0].num;
                            car[i].product[j].tax           = parseFloat(data.product[0].tax).toFixed(2);
                            car[i].product[j].total_tax     = parseFloat(data.product[0].total_tax).toFixed(2);
                            car[i].product[j].total_price   = parseFloat(data.product[0].total_price).toFixed(2);*/
                        }
                        is_product = true;
                        break;
                    }
                }
                if(!is_product){
                    car[i].product.push(data.product[0]);
                }
            }
        }
        if(!is_supplier){
            car.push(data);
        }
    }
    car = JSON.stringify(car);
    localStorage.buy_car = car;
}

/**
 * 立即订购数据存浏览器缓存
 * @param data
 */
function order_now_add(data){
    // var car = localStorage.setItem('order_now','');
    var car = localStorage.getItem('order_now');
    if(null == car || undefined == car || '' == car || [] == car || car =='null'||car =='undefined'){
        car = [];
        car.push(data);
    }else{
        car = JSON.parse(car);
        var is_supplier = false;
        for(var i = 0;i<car.length;i++){
            if(car[i].supplier_id == data.supplier_id){
                is_supplier = true;
                var is_product = false;
                for(var j = 0 ;j<car[i].product.length;j++){
                    if(car[i].product[j].product_id == data.product[0].product_id){
                        if(car[i].product[j].specification==data.product[0].specification){
                            car[i].product[j].num       = parseInt(car[i].product[j].num) + parseInt(data.product[0].num);
                            if (car[i].product[j].num>data.product[0].stock){
                                car[i].product[j].num       =  parseInt(data.product[0].stock);
                                var total_tax = (parseFloat(data.product[0].tax) * parseInt(data.product[0].specification) * parseInt(data.product[0].stock)).toFixed(2);
                                var total_price = (parseFloat(data.product[0].price) * parseInt(data.product[0].specification) * parseInt(data.product[0].stock)).toFixed(2);
                                car[i].product[j].total_tax     =   total_tax;
                                car[i].product[j].total_price   =   (parseFloat(total_tax) + parseFloat(total_price)).toFixed(2);
                            }else {
                                car[i].product[j].total_tax = (parseFloat(car[i].product[j].total_tax)      + parseFloat(data.product[0].total_tax)).toFixed(2);
                                car[i].product[j].total_price = (parseFloat(car[i].product[j].total_price)  + parseFloat(data.product[0].total_price)).toFixed(2);
                            }
                        }else{
                            car[i].product[j].specification = data.product[0].specification;
                            car[i].product[j].price         = data.product[0].price;
                            car[i].product[j].num           = data.product[0].num;
                            car[i].product[j].tax           = (data.product[0].tax).toFixed(2);
                            car[i].product[j].total_tax     = (data.product[0].total_tax).toFixed(2);
                            car[i].product[j].total_price   = (data.product[0].total_price).toFixed(2);
                        }
                        is_product = true;
                        break;
                    }
                }
                if(!is_product){
                    car[i].product.push(data.product[0]);
                }
            }
        }
        if(!is_supplier){
            car.push(data);
        }
    }
    car = JSON.stringify(car);
    localStorage.order_now = car;
}

/**
 * 手机号检验
 * @param phone
 * @returns {boolean}
 */
function checkPhone(phone) {
    if (!(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(phone))) {
        layer.msg('请输入正确的电话号码格式</br>Please enter the correct format of the phone number',{
            skin:'winning-class',
            time:3000
        });
        return false;
    } else {
        return true;
    }
}
/**
 * 电话号码检验
 * @param tel
 * @returns {*}
 */
function checkTel(tel) {
    if(!(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/.test(tel))){
        return false;
    } else {
        return true;
    }
}
/**
 *
 * @param email
 * @returns {boolean}
 */
function checkEmail(email) {
    var szReg=/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
    if (!(szReg.test(email))) {
        layer.msg('请输入正确的邮箱格式</br>Please enter the correct mailbox format',{
            skin:'winning-class',
            time:3000
        });
        return false;
    } else {
        return true;
    }
}